package com.play.ballen.pack.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.yuven.baselib.utils.ResourceUtil

class PackSaleDialogUtils(private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var edPrice: EditText
    private lateinit var edNum: EditText
    private lateinit var tvCountAvailable: TextView
    private lateinit var tvServiceFee: TextView

    private var max = 0
    private var minPrice = 0f

    private val mInputMethodManager by lazy {
        mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    }
    var valuesCallback: ((price: String, num: Int) -> Unit)? = null

    fun show(maxCount: Int, serviceFee: String, min_price: Float) {
        this.max = maxCount
        this.minPrice = min_price
        if (!this::contentView.isInitialized) {
            contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_pack_sale, null)
            contentView.let {
                edPrice = it.findViewById(R.id.edPrice)
                edNum = it.findViewById(R.id.edNum)
                tvCountAvailable = it.findViewById(R.id.tvCountAvailable)
                tvServiceFee = it.findViewById(R.id.tvServiceFee)
                it.findViewById<TextView>(R.id.btnBack).setOnClickListener { btn ->
                    dialog.dismiss()
                }
                it.findViewById<ImageView>(R.id.imgClose).setOnClickListener { btn ->
                    dialog.dismiss()
                }
                it.findViewById<TextView>(R.id.btnOk).setOnClickListener { btn ->
                    try {
                        if (edPrice.text.toString().isEmpty()) {
                            ToastUtils.show(edPrice.hint.toString())
                            return@setOnClickListener
                        }

                        /*if (edNum.text.toString().isEmpty()) {
                            ToastUtils.show(edNum.hint.toString())
                            return@setOnClickListener
                        }*/

                        val price = edPrice.text.toString()
                        /*val num = edNum.text.toString().toInt()

                        if (num == 0) {
                            ToastUtils.show(edNum.hint.toString())
                            return@setOnClickListener
                        }*/

                        if (price.toFloat() < minPrice) {
                            ToastUtils.show(edPrice.hint.toString())
                            return@setOnClickListener
                        }
                        dialog.dismiss()
                        //valuesCallback?.invoke(price, num)
                        valuesCallback?.invoke(price, 1)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

                dialog.setContentView(it)
                dialog.setOnDismissListener {
                    edNum.setText("")
                }
                dialog.setCanceledOnTouchOutside(false)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
                dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
            }
        }

        edPrice.hint = ResourceUtil.getString(R.string.pack_btn_price_tip, minPrice)
        tvCountAvailable.text =
            ResourceUtil.getString(R.string.pack_btn_sale_count_available, "$maxCount")
        tvServiceFee.text = serviceFee

        if (!dialog.isShowing) {
            dialog.show()

            edPrice.post {
                edPrice.requestFocus()
                mInputMethodManager.showSoftInput(edPrice, InputMethodManager.SHOW_IMPLICIT)
            }
        }
    }

}